﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.LogicComponents {
    import flash.geom.*;
    import Engine.Helpers.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Classes.effects.*;
    import Engine.*;
    import Modules.stats.types.*;

    public class HarvestableShipComponentBase {

        protected var m_ship:HarvestableShip;
        protected var m_shipNPC:SeaVehicle;
        protected var m_returnTime:Number = -1;
        protected var m_shipReturnTrigger:Boolean = false;

        public function HarvestableShipComponentBase(_arg1:HarvestableShip){
            this.m_ship = _arg1;
        }
        public function getProgressBarEndFunction():Function{
            return (function ():void{
                if (Global.isVisiting()){
                    return;
                };
                if (m_ship.harvest()){
                    m_ship.doHarvestDropOff();
                    startShipLeave();
                };
            });
        }
        public function getProgressBarStartFunction():Function{
            return (null);
        }
        public function getProgressBarCancelFunction():Function{
            return (function ():void{
            });
        }
        public function harvest():Boolean{
            var _local1:Boolean;
            if (this.m_ship.isHarvestable()){
                if (!Global.isVisiting()){
                    this.m_ship.doDoobers();
                    if (this.m_ship.actionMode != this.m_ship.VISIT_REPLAY_ACTION){
                        GameTransactionManager.addTransaction(new THarvest(this.m_ship));
                        this.m_ship.trackAction(TrackedActionType.HARVEST);
                    };
                };
                this.m_ship.setState(HarvestableResource.STATE_PLANTED);
                this.m_ship.plantTime = GlobalEngine.getTimer();
                _local1 = true;
            };
            this.m_ship.harvestCounter++;
            this.m_ship.updateObjectIndicator();
            return (_local1);
        }
        public function handlePlayAction():void{
            var _local1:Number;
            if (((!(Global.isVisiting())) && ((this.m_ship.getState() == HarvestableResource.STATE_GROWN)))){
                _local1 = this.m_ship.harvestingDefinition.harvestEnergyCost;
                if (!Global.player.checkEnergy(-(_local1))){
                    this.m_ship.displayStatus(ZLoc.t("Main", "NotEnoughEnergyRed"));
                } else {
                    this.m_ship.doEnergyChanges(-(_local1), new Array("energy", "expenditures", "harvest_ship", this.m_ship.harvestingDefinition.name));
                    this.m_ship.removeStagePickEffect();
                    Global.world.citySim.pickupManager.enqueue("NPC_shipPickup", this.m_ship);
                };
            };
        }
        public function onUpdate(_arg1:Number):void{
            if (this.m_ship.getState() == HarvestableResource.STATE_PLANTED){
                this.calculateShipReturnTime();
                this.checkAndDoShipReturns();
            } else {
                if ((((this.m_ship.getState() == HarvestableResource.STATE_GROWN)) && (this.m_shipReturnTrigger))){
                    this.m_shipReturnTrigger = false;
                };
            };
        }
        public function updateState(_arg1:Number):Boolean{
            if (((this.m_ship.isHarvestable()) && ((this.m_ship.getState() == HarvestableResource.STATE_PLANTED)))){
                this.m_ship.setState(HarvestableResource.STATE_GROWN);
                return (true);
            };
            return (false);
        }
        protected function startShipLeave():void{
            var _local1:int = Global.gameSettings.getInt("shipExitX", -80);
            var _local2:Array = new Array();
            _local2.push(new Vector3(_local1, this.m_ship.getPosition().y, 0));
            var _local3:Item = Global.gameSettings.getItemByName(this.m_ship.harvestingDefinition.emptyNPCType);
            var _local4:Number = _local3.navigateXml.walkSpeed;
            this.m_shipNPC = Global.world.citySim.npcManager.createShipByNameAtPosition(this.m_ship.harvestingDefinition.emptyNPCType, this.m_ship.getPosition(), _local2, false, _local4);
        }
        protected function startShipArrive():void{
            this.m_shipReturnTrigger = true;
            var _local1:int = Global.gameSettings.getInt("shipExitX", -80);
            var _local2:Vector3 = new Vector3(_local1, this.m_ship.getPosition().y, 0);
            var _local3:Array = new Array();
            _local3.push(new Vector3(this.m_ship.getPosition().x, this.m_ship.getPosition().y, 0));
            var _local4:Item = Global.gameSettings.getItemByName(this.m_ship.harvestingDefinition.loadedNPCType);
            var _local5:Number = _local4.navigateXml.walkSpeed;
            this.m_shipNPC = Global.world.citySim.npcManager.createShipByNameAtPosition(this.m_ship.harvestingDefinition.loadedNPCType, _local2, _local3, false, _local5);
        }
        protected function calculateShipReturnTime():Number{
            if (this.m_returnTime != -1){
                return (this.m_returnTime);
            };
            var _local1:int = Global.gameSettings.getInt("shipExitX", -80);
            var _local2:Vector3 = new Vector3(_local1, this.m_ship.getPosition().y, 0);
            var _local3:Number = MathUtil.distance(new Point(this.m_ship.getPosition().x, this.m_ship.getPosition().y), new Point(_local2.x, _local2.y));
            var _local4:Item = Global.gameSettings.getItemByName(this.m_ship.harvestingDefinition.loadedNPCType);
            var _local5:Number = _local4.navigateXml.walkSpeed;
            this.m_returnTime = (_local3 / _local5);
            return (this.m_returnTime);
        }
        protected function checkAndDoShipReturns():void{
            var _local1:Number;
            var _local2:Number;
            var _local3:Number;
            if (this.m_ship.plantTime > 0){
                _local1 = (GlobalEngine.getTimer() - this.m_ship.plantTime);
                _local2 = this.m_ship.getGrowTimeDelta();
                if (_local1 < _local2){
                    _local3 = ((_local2 - _local1) / 1000);
                    if ((((_local3 < this.m_returnTime)) && (!(this.m_shipReturnTrigger)))){
                        this.startShipArrive();
                    };
                };
            };
        }
        public function getToolTipStatus():String{
            var _local1:String;
            if (this.m_ship.getState() == HarvestableResource.STATE_PLANTED){
                _local1 = GameUtil.formatMinutesSeconds(this.m_ship.getGrowTimeLeft());
                return (ZLoc.t("Main", "AwayShip", {time:_local1}));
            };
            return ("");
        }
        public function getToolTipAction():String{
            return (this.m_ship.getGameModeToolTipAction());
        }
        public function getActionText():String{
            return (ZLoc.t("Main", "Unloading"));
        }
        public function getHarvestTime():Number{
            return (Global.gameSettings.getNumber("actionBarHarvest"));
        }
        public function createStagePickEffect():void{
            if (this.m_ship.getState() == HarvestableResource.STATE_GROWN){
                this.createStagePickEffectHelper(StagePickEffect.PICK_1);
            };
        }
        protected function createStagePickEffectHelper(_arg1:String):void{
            var _local2:StagePickEffect = this.m_ship.stagePickEffect;
            if (!_local2){
                _local2 = (MapResourceEffectFactory.createEffect(this.m_ship, EffectType.STAGE_PICK) as StagePickEffect);
                _local2.setPickType(_arg1);
                _local2.float();
            } else {
                _local2.setPickType(_arg1);
                _local2.reattach();
                _local2.float();
            };
            this.m_ship.stagePickEffect = _local2;
        }
        public function enableUpdateArrow():Boolean{
            return (((!(Global.isVisiting())) && ((this.m_ship.getState() == HarvestableResource.STATE_GROWN))));
        }
        public function checkPlacementRequirements(_arg1:int, _arg2:int):Boolean{
            var _local5:Pier;
            var _local6:Array;
            var _local3:Array = Global.world.getObjectsByClass(Pier);
            var _local4:Vector3 = this.m_ship.getSize();
            for each (_local5 in _local3) {
                if (_local5.shipCollidedWithDock(new Rectangle(_arg1, _arg2, _local4.x, _local4.y))){
                    return (false);
                };
            };
            _local6 = Global.world.findConnectingMapResources(_arg1, _arg2, _local4.x, _local4.y, null, false, Dock);
            if (_local6.length > 0){
                return (true);
            };
            return (false);
        }
        public function isHarvestable():Boolean{
            return ((((((this.m_ship.getGrowPercentage() == 100)) && (!((this.m_ship.getState() == HarvestableResource.STATE_FALLOW))))) || ((this.m_ship.getState() == HarvestableResource.STATE_GROWN))));
        }
        public function makeDoobers(_arg1:Item, _arg2:Array, _arg3:Number=1):Array{
            return (Global.player.processRandomModifiers(_arg1, this.m_ship, true, _arg2));
        }
        public function onStateChanged(_arg1:String, _arg2:String):void{
        }
        public function setPlantedWhenShipArrives():void{
            var _local1:Number = GlobalEngine.getTimer();
            var _local2:Number = this.m_ship.getGrowTimeDelta();
            this.m_ship.plantTime = ((_local1 - _local2) + (this.calculateShipReturnTime() * 1000));
        }
        public function isSellable():Boolean{
            return (true);
        }

    }
}//package Classes.LogicComponents 
